<template>
  <div>
    <x-header id="head_top" :left-options="{showBack: false}">
      培训台账
      <a slot="right">{{userName}}</a>
    </x-header>
    <div>
      <cell title = "台账移动端" @click.native="onClick" ></cell>
      <cell title = "个人履历" @click.native="onClickR"></cell>
    </div>
  </div>
</template>
<script>
import "assets/iconfont/iconfont.css";
import vue from "vue";
import { Loadmore } from "mint-ui";
vue.component(Loadmore.name, Loadmore);
import {
  getDDUserInfo,
  getUser
} from "src/service/actual";
import {
  XHeader,
  Cell,
} from "vux";
import { isNullOrUndefined } from "util";
export default {
  components: {
    Cell,
    XHeader,
  },
  data() {
    return {
      bpt: "上拉加载更多",
      tabIndex: 0,
      page: 1,
      nums: 10, //每页显示数量
      allLoaded: false, //是否加载完所有内容
      activeName: "0",
      state: "",
      infocode: "",
      noData: "",
      userInfo: null,
      userCode: "",
      searchDept: "",
      userName:"",
    };
  },
  computed: {},
  watch: {
    infocode: function(newvalue, oldvale) {
      this.getUserInfo();
      console.log("infocode-");
    },
    userCode: function(newvalue, oldvale) {
      this.getUser();
    }
  },
  mounted() {
  },
  created() {
    console.log("created");
    this.getDDInfo();
    // this.getUserInfo();
  },
  methods: {
    getDDInfo() {
      if (process.env.NODE_ENV == "development") {
        this.infocode = "123";
        console.log("getDDInfo-development");
      } else {
        var _this = this;
        var ddurl = document.URL.split("#")[0];
        var query = { ddurl: ddurl };
        var dd = window.dd;
        var res_corpId = "ding786250c40f654fe4";
        console.log("dd", dd);
        dd.ready(function() {
          dd.runtime.permission.requestAuthCode({
            corpId: "ding786250c40f654fe4",
            onSuccess: function(info) {
              console.log("dd_info_code:" + info.code);
              _this.infocode = info.code;
            },
            onFail: function(err) {
              alert("requestAuthCode——登录验证失败，请联系管理员。");
            }
          });
        });
      }
      console.log("this.infocode :" + this.infocode);
    },
    // 通过dingding接口，获取用户信息
    async getUserInfo() {
      if (this.infocode) {
        getDDUserInfo(this.infocode).then(response => {
          if (response.status == 200) {
            this.userInfo = response.data;
            console.log("getUserInfo-userInfo", this.userInfo);
            this.userCode = this.userInfo.user_code;
            this.userName=this.userInfo.user_name;
            // this.$store.commit("setUser", this.userInfo);
          } else {
            alert("数据加载失败，请重试！");
          }
        });
      }
    },
    //  根据登录账号(工号、姓名)，从后台数据库获取用户信息（部门、电话、岗位？）
    async getUser() {
      getUser().then(response => {
        if (response.status == 200) {
          this.userInfo = response.data;
             this.$store.commit("setUser", this.userInfo);    //2019.8.21 更，从后台获取用户数据（工号、姓名、岗位、部门）后存储
          console.log("getUser-userInfo",this.userInfo);
        } else {
          alert("数据加载失败，请重试！");
        }
      });
    },
    onClick(){
      this.$router.push({
        path: "/mhome"
      });
    },
    onClickR(){
      this.$router.push({
        path: "/phome"
      });
    }
  }
};
</script>
<style lang="less" scoped>
@import "~vux/src/styles/1px.less";
@import "~vux/src/styles/center.less";
#head_top {
  background-color: #1e90ff;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 2.95rem;
  z-index: 1;
}
.div_head {
  position: fixed;
  background-color: #fff;
  left: 0;
  top: 3rem;
  width: 100%;
  z-index: 1;
}
.box {
  padding: 15px;
}
.icon-daiban {
  font-size: 25px;
}
.icon-yiban {
  font-size: 25px;
}
.icon-quanbu {
  font-size: 25px;
}
</style>